const app = getApp()
const log =require('../../log.js');
const util=require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    matchName:'',
    matchId:'',
    idcard:'',
    username:'',
    result:0,
    show:0


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('-----options start------');
    console.log(options);
    console.log('------options end-----');
    var date=util.formatTime(new Date());
    log.info("Time:"+date+",match:match,options:"+JSON.stringify(options));
    log.info("Time:"+date+",match:match,open:"+JSON.stringify(open));
    var matchName = options.matchname;
    var matchId = options.match_id;
    var shared=options.share;
    var from=options.from;
    if(from==undefined){
     this.setData({
       from:0
     })
    }else{
     this.setData({
       from:1
     })
    }
    if(shared==undefined){
       shared=0;
   }else{
      shared=1;
   }
   wx.showShareMenu({
     menus: ['shareAppMessage', 'shareTimeline'],
     success(res) {
       console.log(res)
     },
     fail(e) {
       console.log(e)
     }
   })   
  this.data.matchName=matchName;
  this.data.matchId=matchId;
    var open=wx.getStorageSync('open');
    console.log(open);
    var member_id=open.member_id;
    var id=open.id;
    if(open==undefined ){
 
     wx.navigateTo({
       url: '../index/login',
     })
    }
    if(open==""){
 
     wx.navigateTo({
       url: '../index/index',
     })
    }
   var that = this;
   this.setData({
    matchName:matchName,
    matchId:matchId,
      domain:app.globalData.domain,
      width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop
  })
  },
  validName:function(e){
    if(e){
      if (!(/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(e))) {
        return false;
      }else{
        return true;
      }
   }else{
      return false;
   }

  },
  validIdcardNum:function(e){
    if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(e))) {
     return false;
    }else{
      return true;
    }
  },
  idCard(e){
    console.log(e)
    this.setData({
      idcard:e.detail.value
    })
  },
  userName(e){
    console.log(e)
    this.setData({
      username:e.detail.value
    })
  },
  search(){
    
    let that=this;
    let data=this.data;
    var idcard=data.idcard;
    var username=data.username;
    var matchId=data.matchId;
    var open=wx.getStorageSync('open');
    var user_id=open.id;
    console.log(matchId);
    if(this.validName(username)){
      if(this.validIdcardNum(idcard)){
        if(matchId){         
            wx.request({
              url:app.globalData.domain+'match/match/matchScore',    
              data: {matchId:matchId,username:username,idcard:idcard },
              success: function (res) {
                console.log(res)
                if(res.statusCode==200){
                    that.setData({
                      code:res.data.code,
                      info:res.data.data
                    })
                }
              }            
              
            })
            
          
        }else{
          this.showModels('赛事信息不正确');
        }
      }else{
        this.showModels('身份证号码不正确');
      }
    }else{
      this.showModels('姓名没有输入');
    }
    

  },
createCert(){
  let that=this;
  let data=this.data;

  wx.request({
    url: app.globalData.domain+'match/match/createCert',
  data:{matchId:data.matchId,id:data.info.id,from:3},
  success:function(res){
    console.log(res);
    that.setData({
      url:res.data,
      show:1
    })
  }
  })
},
closePoster(){
  this.setData({
      show:false
  })
},
save () {
  let that = this
  //若二维码未加载完毕，加个动画提高用户体验
  wx.showToast({
   icon: 'loading',
   title: '正在保存图片',
   duration: 1000
  })
  //判断用户是否授权"保存到相册"
  wx.getSetting({
   success (res) {
    //没有权限，发起授权
    if (!res.authSetting['scope.writePhotosAlbum']) {
     wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success () {//用户允许授权，保存图片到相册
       that.savePhoto();
      },
      fail () {//用户点击拒绝授权，跳转到设置页，引导用户授权
       wx.openSetting({
        success () {
         wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
           that.savePhoto();
          }
         })
        }
       })
      }
     })
    } else {//用户已授权，保存到相册
     that.savePhoto()
    }
   }
  })
 },
//保存图片到相册，提示保存成功
 savePhoto() {
  let that = this
  var url=this.data.url;
  wx.downloadFile({
   url: url,
   success: function (res) {
    wx.saveImageToPhotosAlbum({
     filePath: res.tempFilePath,
     success(res) {
      wx.showToast({
       title: '保存成功',
       icon: "success",
       duration: 1000
      })
     }
    })
   }
  })
 },
  goBack(){
    wx.navigateBack();
  },
  showModels:function(msg){
    wx.showModal({
        title: '系统提示',
        content:msg,
        success:function(res){
            return false;
        }
    })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})